package cn.kxm.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import cn.kxm.pojo.Dict;
import cn.kxm.pojo.Part;
import cn.kxm.pojo.PartDetail;
import cn.kxm.pojo.PartType;
import cn.kxm.pojo.Storageinfo;
import cn.kxm.service.DictService;
import cn.kxm.service.PartDetailService;
import cn.kxm.service.PartService;
import cn.kxm.service.PartTypeService;

@Controller
@RequestMapping("/part")
public class PartController{
    @Autowired
	private DictService dictService;
	@Autowired
	private PartService partService;
	@Autowired
	private PartTypeService partTypeService;
	@Autowired
	private PartDetailService detailService;
	
	@RequestMapping("getPartList")
	@ResponseBody
	public Object getPartList(Integer rows,Integer page,Part part){
		System.out.println(part.getPartBrand());
		List<Part>list=partService.getPartList(part.getPartBrand(),part.getPartNo(),part.getPartName(),part.getPartTypeId(), page, rows);
		Map<String, Object>map=new HashMap<String, Object>();
		map.put("rows", list);
		map.put("total", partService.getPartCount(part.getPartBrand(),part.getPartNo(),part.getPartName(),part.getPartTypeId()));
		return JSON.toJSON(map);
	}
	
	@RequestMapping("getAllpartType")
	@ResponseBody
	public Object getAllpartType(){
		List<PartType>list=partTypeService.getAllPartType();
		return JSON.toJSON(list);
	}
	@RequestMapping("getByCode")
	@ResponseBody
	public Object getByCode(String code){
		List<Map<String, Object>>list=partService.getByCode(code);
		return JSON.toJSON(list);
	}
	@RequestMapping("getPart_Ib")
	@ResponseBody
	public Object getPart_Ib(String row){
		
		return "";
	}
	@RequestMapping("getPartDetailList.json")
	@ResponseBody
	public Object getPartDetailList(@RequestParam(value="page",required=false)Integer page,
			                        @RequestParam(value="rows",required=false)Integer rows,
			                        Part part,
			                        @RequestParam(value="mold",required=false)String mold){
		Map<String, Object>maps=new HashMap<String, Object>();
		maps.put("total", partService.getPartDetailListCount(part, mold));
		maps.put("rows", partService.getPartDetailList(page, rows, part, mold));
		return JSON.toJSONString(maps);
	}
	@RequestMapping("getPartTypeListBrand.json")
	@ResponseBody
	public Object getPartTypeListBrand(String type){
		Dict dict=new Dict();
		dict.setValue(0);
		dict.setKey("--配件品牌--");
		List<Dict> lists=dictService.getDictListByType(type);
		lists.add(0, dict);
		return JSON.toJSONString(lists);
	}
	@RequestMapping("getPartTypeListUnit.json")
	@ResponseBody
	public Object getPartTypeListUnit(String type){
		List<Dict> lists=dictService.getDictListByType(type);
		return JSON.toJSONString(lists);
	}
	@RequestMapping("addPartDetail.json")
	@ResponseBody
	public Object addPartDetail(@RequestParam("detail")String detail){
		JSONObject partJson=JSONObject.parseObject(detail);
		Part part=new Part();
		part.setCode(partJson.getString("code"));
		part.setMemo(partJson.getString("memo"));
		if(!partJson.getString("partBrand").equalsIgnoreCase("--配件品牌--")){
			part.setPartBrand(partJson.getString("partBrand"));
		}
		part.setPartName(partJson.getString("partName"));
		part.setPartNo(partJson.getString("partNo"));
		part.setPartTypeId(partJson.getInteger("partTypeId"));
		part.setPartUnit(partJson.getString("partUnit"));
		part.setSellPrice(partJson.getDouble("sellPrice")==null?0.00:partJson.getDouble("sellPrice"));
		part.setSize(partJson.getString("size"));
		part.setWeight(partJson.getString("weight"));
		PartDetail detail1=new PartDetail();
		detail1.setCreationId(2);
		detail1.setCurrPartNo(partJson.getString("currPartNo"));
		detail1.setDisplayer(partJson.getInteger("displayer"));
		detail1.setMold(partJson.getString("mold"));
		detail1.setOldMold(partJson.getString("oldMold"));
		detail1.setPartCode(part.getCode());
		Storageinfo info=null;
		if(partJson.getInteger("instroage")==1){
			info=new Storageinfo();
			info.setAmount(partJson.getInteger("amount"));
			info.setPartCode(part.getCode());
			info.setSellPrice(part.getSellPrice());
			info.setStroragePrice(partJson.getDouble("price"));
		}
		Map<String, Object> maps=new HashMap<String, Object>();
		if(partService.addPart(part, detail1,info)){
			maps.put("result", "success");
		}else{
			maps.put("result", "error");
		}
		return JSON.toJSONString(maps);
	}
	@RequestMapping("modifyPartDetail.html")
	public String modifyPartDetail(String code,Model model){
		model.addAttribute("part", partService.getPartByCode(code));
		model.addAttribute("detail",detailService.getPartDetailByPartCode(code));
		return "web/goods/modifyPartDetail";
	}
	@RequestMapping("modifyPartDetail.json")
	@ResponseBody
	public Object modifyPartDetail(@RequestParam("detail")String detail){
		JSONObject partJson=JSONObject.parseObject(detail);
		Part part=new Part();
		part.setCode(partJson.getString("code"));
		part.setMemo(partJson.getString("memo"));
		if(!partJson.getString("partBrand").equalsIgnoreCase("--配件品牌--")){
			part.setPartBrand(partJson.getString("partBrand"));
		}
		part.setPartName(partJson.getString("partName"));
		part.setPartNo(partJson.getString("partNo"));
		part.setPartTypeId(partJson.getInteger("partTypeId"));
		part.setPartUnit(partJson.getString("partUnit"));
		part.setSellPrice(partJson.getDouble("sellPrice")==null?0.00:partJson.getDouble("sellPrice"));
		part.setSize(partJson.getString("size"));
		part.setWeight(partJson.getString("weight"));
		PartDetail detail1=new PartDetail();
		detail1.setCreationId(2);
		detail1.setCurrPartNo(partJson.getString("currPartNo"));
		detail1.setDisplayer(partJson.getInteger("displayer"));
		detail1.setMold(partJson.getString("mold"));
		detail1.setOldMold(partJson.getString("oldMold"));
		detail1.setPartCode(part.getCode());
		Map<String, Object> maps=new HashMap<String, Object>();
		if(partService.modifyPartByPart(part, detail1)){
			maps.put("result", "success");
		}else{
			maps.put("result", "error");
		}
		return JSON.toJSONString(maps);
	}
	@RequestMapping("delrows.json")
	@ResponseBody
	public Object delPart(String codes){
		Map<String, Object> maps=new HashMap<String, Object>();
		if(partService.delPartByCode(codes)){
			maps.put("result", "success");
		}else{
			maps.put("result", "error");
		}
		return JSON.toJSONString(maps);
	}
}
